#!/bin/bash

#
# Do not run coverage for forks as most people do not have extra tools enabled
# for their fork.
#
extra_args=""
if [ "x$TRAVIS_REPO_SLUG" == "xoracle/opengrok" ]; then
	echo "Enabling Coveralls"
	extra_args="jacoco:report coveralls:report"

	#
	# Sonar seems to only work on master branch commits.
	#
	if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then
		echo "Enabling Sonar"
		extra_args="$extra_args sonar:sonar"
	fi
fi

if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
	JAVA_BASE="/c/Program Files/OpenJDK"
	JDK_DIR=$(find "$JAVA_BASE/" -type d -name 'openjdk*' -maxdepth 1 | head -1)
	export JAVA_HOME=${JAVA_HOME:-$JDK_DIR}
	echo "JAVA_HOME set to $JAVA_HOME"
	export PATH=${JAVA_HOME}/bin:${PATH}

	export PATH=${PATH}:/c/ProgramData/chocolatey/lib/universal-ctags/tools
	export PATH=${PATH}:/c/Program\ Files\ \(x86\)/Subversion/bin
	export PATH=${PATH}:${JAVA_HOME}

	# needs to be before the default Python 2
	export PATH=/c/Python38:${PATH}

	extra_args="$extra_args -Dorg.opengrok.indexer.analysis.Ctags=c:\\ProgramData\\chocolatey\\lib\\universal-ctags\\tools\\ctags.exe"
fi

ret=0
./mvnw -B -V verify $extra_args || ret=1

if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
	echo "Checking Apiary blueprint format"
	node dev/parse.js || ret=1
fi

exit $ret
